//
//  RGMainPageViewController.m
//  RedGrass
//
//  Created by wukong on 2018/11/24.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMainPageViewController.h"
#import "RGMainPageView.h"
#import "RGShareView.h"
#import "RGCommentView.h"
#import "RGMainPageViewModel.h"
#import "RGCommentToolBarView.h"


@interface RGMainPageViewController ()<RGMainPageViewDelegate,RGShareViewDelegate,RGCommentViewDelegate,RGCommentToolBarViewDelegate>

@property (strong, nonatomic) RGMainPageView *pageView;
@property (strong, nonatomic) RGShareView *shareView;
@property (strong, nonatomic) RGCommentView *commentView;
@property (strong, nonatomic) RGMainPageViewModel *viewModel;
@property (strong, nonatomic) RGCommentToolBarView *toolBarView;
@property (assign, nonatomic) BOOL isFirstLoad;

@end

@implementation RGMainPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHiddenNavBar = YES;
    _isFirstLoad = YES;
    if (@available(iOS 11.0, *)) {
        //        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    if (!IS_iPhoneX) {
        [self.customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.view).mas_offset(0);
        }];
    }
    [self.customContentView addSubview:self.pageView];
    [_pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//    [self.viewModel changeTabbarUiStyleWithChosedVedioIndex:_pageView.currentShowCellIndex];
}

-(void) viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    if(_isFirstLoad){
         [self.viewModel gainRecommendVideoListWithPageNumIsFirstLoad:YES isLoadMore:NO];
        _isFirstLoad = NO;
    }
}

-(void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_pageView stopPlayingWhenCurrentVCIsNoteVisiable];
}

#pragma mark - viewDelegate

-(void)gainRecommendVideoListWithView:(RGMainPageView *)view isLoadMore:(BOOL)isLoadMore{
    [self.viewModel gainRecommendVideoListWithPageNumIsFirstLoad:NO isLoadMore:isLoadMore];
}

//搜索
-(void) searchSomethingWithView:(RGMainPageView *)view{
   [self.viewModel showSearchViewController];
}

//扫一扫
-(void)scanQRCodeWithView:(RGMainPageView *)view{
    [self.viewModel showScanViewController];
}

//点击他人图像
-(void) userIconClickWithCellIndex:(NSInteger)index{
   // if([self checkIsHaveToken]){
//      [_pageView stopPlayingWhenCurrentVCIsNoteVisiable];
        [self.viewModel pushToPersonalVcWithChosdeVedioIndex:index];
   // }else{
        
   // }
}

//分享
-(void) shareWithView:(RGMainPageView *)view selectedPath:(NSIndexPath *)path{
  //  if([self checkIsHaveToken]){
        self.zh_popupController = [zhPopupController new];
        self.zh_popupController.layoutType = zhPopupLayoutTypeBottom;
        self.zh_popupController.dismissOnMaskTouched = YES;
        self.zh_popupController.allowPan = YES;
        self.zh_popupController.slideStyle = zhPopupSlideStyleFromBottom;
        self.zh_popupController.maskAlpha = 0.2;
        
        //    [self.shareView initTopScrollViewWithUserArr:_testArr];
        self.shareView.chosedVedioIndex = path.row;
        [self.zh_popupController presentContentView:self.shareView duration:0.25 springAnimated:NO];
  //  }else{
      
  //  }
   
}

-(void) shareChosedWithShareView:(RGShareView *)view shareType:(RGShareType)type chosedIndex:(NSInteger)chosedIndex{
    NSLog(@"");
    [self.zh_popupController dismiss];
}

-(void) shareChosedWithView:(RGShareView *)view chosedIndex:(NSInteger)chosedIndex{

    [self.zh_popupController dismiss];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
        if(chosedIndex == kShareMessageTag){//私信
            [self.viewModel pushToChatVcWithChosedVedioIndex:view.chosedVedioIndex];
        }else if (chosedIndex == kSharePushToOtherTag){//转发
            self.toolBarView.currentIndex = view.chosedVedioIndex;
            [self.toolBarView show];
        }else if (chosedIndex == kShareToOtherTag){
            [self.viewModel shareToWeChatWithChosedVedioIndex:view.chosedVedioIndex];
        }else if (chosedIndex == kShareSaveTag){
            [self.viewModel downLoadVedioWithChosedVedioIndex:view.chosedVedioIndex];
        }
    });
}

-(void) closeViewWithShareView{
    [self.zh_popupController dismiss];
}

//评论
-(void) commentWithView:(RGMainPageView *)view selectedPath:(NSIndexPath *)path{
    
    //if([self checkIsHaveToken]){
        _commentView = nil;
        _commentView.commentViewdelegate = nil;
        [self.commentView show];
        AA_WEAK_MAKE(self)weakSelf = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5), dispatch_get_main_queue(), ^{
            [weakSelf.viewModel gainVideoRecommentListWithChosedIndex:path.row isGainMore:NO isShowLoading:NO];
        });
  //  }
}



//关注，取消关注
-(void) focusWithView:(RGMainPageView *)view selectedPath:(NSIndexPath *)path{
//     if([self checkIsHaveToken]){
         [self.viewModel changeFocusStatusWithChosedIndex:path.row];
//     }
}

//喜欢
-(void) favoriteWithView:(RGMainPageView *)view selectedPath:(NSIndexPath *)path{
//     if([self checkIsHaveToken]){
         [self.viewModel changeFavoriteStautswithChosedIndex:path.row];
//     }
}

//评论点赞

-(void) favoriteCommentWithView:(RGCommentView *)view chosedIndex:(NSInteger)index{
    [self.viewModel changeMainCommentFavoriteStautswithChosedIndex:index];
}

-(void) favoriteChildCommentWithView:(RGCommentView *)view chosedIndexPath:(NSIndexPath *)path{
    [self.viewModel changeChildCommentFavoriteStautswithChosedIndex:path];
}

//发表评论
-(void) addMainCommentWithView:(RGCommentView *)view content:(NSString *)content{
    [self.viewModel addMainCommentWithContent:content];
}

//发表子评论
-(void) addChildCommentWithView:(RGCommentView *)view path:(NSIndexPath *)currentPath chosedIndex:(NSInteger)index content:(NSString *)content{
    [self.viewModel addChildCommentWithContent:content chosedIndexPath:currentPath chosedIndex:index];
}

-(void) loadMainCommentListWithView:(RGCommentView *)view{
    [self.viewModel gainVideoRecommentListWithIsGainMore:NO];
}

-(void) loadMoreMainCommentListWithView:(RGCommentView *)view{
    [self.viewModel gainVideoRecommentListWithIsGainMore:YES];
}

//转发
-(void) onSendText:(NSString *)text WithView:(RGCommentToolBarView *)view{
    if([text length] && ![text isEqualToString:@"\n"]){
        //调用转发接口
        [self.viewModel shareVideoWithContent:text chosedIndex:view.currentIndex];
    }else{
        [self showTextMessageWithMessage:@"不能发送空内容"];
    }
}

//播放量
-(void) updateVedioPlayCountWithCellIndex:(NSInteger)index{
    [self.viewModel updateVeidoPlayCountsWithChosedVedioIndex:index];
}

-(void) updateTabbarUIStyleWithCellIndex:(NSInteger)index{
//    [self.viewModel changeTabbarUiStyleWithChosedVedioIndex:index];
}


#pragma mark -view protocol

//视频相关
-(void) reloadTableViewWithVedioList:(NSMutableArray *)list{
    [_pageView reloadTableViewWithVedioList:list];
//    [self.viewModel changeTabbarUiStyleWithChosedVedioIndex:0];
}

//更多视频的时候刷新
-(void) reloadTableViewWhenHaveMoreVedioWithVedioList:(NSMutableArray *)vedioList{
    [_pageView reloadTableViewWhenHaveMoreVedioWithVedioList:vedioList];
}

-(void) viewMjFooterEndLoading{
    [_pageView mainPageViewMjFooterLoadingEnd];
}

-(void) viewMjHeaderEndLoading{
    [_pageView mainPageViewMjHeaderLoadingEnd];
}

-(void) viewMjFooterStatusWithNoMoreData{
    [_pageView setMainPageViewMjFooterStatusWithNoMoreData];
}

-(void) playWithFirstIndexWhenLoadSuccess{
    [_pageView playVedioWithFirstIndexWhenLoadSuccess];
}

-(void) changeFocusStatusWithIsFocus:(BOOL)isFocus currentCellIndex:(NSInteger)index{
   
    [_pageView startFocusAnimationWithIsFocus:isFocus cellIndex:index];
}

-(void) changeFavoriteStatusWithIsFavorite:(BOOL)isFavorite currentCellIndex:(NSInteger)index{
    [_pageView startFavoriteAnimationWithIsFavorite:isFavorite cellIndex:index];
}


//评论成功 或者评论请求成功后，更新视图里面的评论数,
-(void) updateCommentCountsWhenCommentSuccessWithCellIndex:(NSInteger)cellIndex{
    [_pageView updateCommentCountsWhenCommentSuccessWithCellIndex:cellIndex];
}

//转发成功后更新转发数
-(void)updateforwardCountsWhenShareSuccessWithCellIndex:(NSInteger)cellIndex{
    [_pageView updateforwardCountsWhenShareSuccessWithCellIndex:cellIndex];
}

//评论相关

-(void) reloadCommentTableViewWithCommentList:(NSMutableArray *)commentList{
    
    [_commentView reloadCommentTabelViewWithCommentList:commentList];
}

//点赞后刷新section
-(void) reloadCommentTableViewSectionWithIndex:(NSInteger)index{
    [_commentView changeFarvoriteStatusWithIndex:index];
}

//点赞子评论后刷新cell
-(void) reloadCommentTableViewCellWithIndexPath:(NSIndexPath *)path{
    [_commentView changeFarvoriteStatusWithIndexPath:path];
}

-(void) resetCommentStatusWhenPublicSuccess{
    [_commentView resetCommentStauts];
}

-(void) commentMjFooterEndLoading{
    [_commentView commentMjFooterLoadingEnd];
}

-(void) commentMjHeaderEndLoading{
    [_commentView commentMjHeaderLoadingEnd];
}

-(void) changeCommentMjFooterStatusWithNoMoreData{
    [_commentView setCommentMjFooterStatusWithNoMoreData];
}

-(void) showAlertWhenFarvoriteSelfComment{
    [_commentView showAlertWhenFarvoriteSelfComment];
}

-(void) showAlertWhenCommentSelfComment{
    [_commentView showAlertWhenCommentSelfComment];
}


//dismisstoolbarview
-(void) dismissToolBarView{
    [_toolBarView dismiss];
}

#pragma mark - private

//检查是否有token
-(BOOL)checkIsHaveToken{
    return [[RGUserInfoModel getUserInfo].token length]> 0 ? YES : NO;
}


#pragma mark -getter
-(RGMainPageView *)pageView{
    if(!_pageView){
        _pageView = [[RGMainPageView alloc]init];
        _pageView.viewDelegate = self;
    }
    return _pageView;
}

-(RGShareView *)shareView{
    if(!_shareView){
        CGFloat Height = KIsiPhoneX ? (150 + 34):150;
        _shareView = [[RGShareView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, Height)];
        _shareView.shareViewDelegate = self;
    }
    return _shareView;
}

-(RGCommentView *)commentView{
    if(!_commentView){
        _commentView = [[RGCommentView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        _commentView.commentViewdelegate = self;
    }
    return _commentView;
}

-(RGCommentToolBarView *)toolBarView{
    if(!_toolBarView){
        _toolBarView = [[RGCommentToolBarView alloc]init];
        _toolBarView.isShowBackGround = YES;
        _toolBarView.delegate = self;
    }
    return _toolBarView;
}


-(RGMainPageViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[RGMainPageViewModel alloc]initWithProtocol:self];
    }
    return _viewModel;
}

-(void) dealloc{
     [_pageView stopPlayingWhenCurrentVCIsNoteVisiable];
}


@end
